#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include <windows.h>
struct skill {//人物技能
	char skillName[21];//技能名
	int hurtValue;//造成的伤害
	int blueDeplete;//耗蓝
	int ID;
	int token;
};
struct equipment {//装备
	char equipmentName[21];//装备命名
	int fightValue;//攻击加成
	int token;
};
struct player {
	char playerName[21];//人物名字
	char rank[7];//等级
	int time;//游戏时间
	int blood;//血量
	int blue;//蓝
	int fightPlayer;//攻击力
	int money;//灵石
	int exp;//经验
	struct skill playerSkill[7];
	struct equipment playerEquipment;
};
struct monster {
	char monsterName[10];//怪物名
	char monsterRank[10];//怪物境界
	int monsterBlood;//血量
	int monsterfight;//攻击力
	int exp;//经验
	int token;
	char word[100];//台词
	struct monster* next;
};
int ret=1,pass=0;
const char FirstpassFile[] = "./dataBase/1.csv";///////////////////
const char SecondpassFile[] = "./dataBase/2.csv";  //
const char ThirdlypassFile[] = "./dataBase/3.csv"; //
const char FourthpassFile[] = "./dataBase/4.csv";  //保存数据文件路径
const char FifthpassFile[] = "./dataBase/5.csv";   //
const char SixthpassFile[] = "./dataBase/6.csv";   //
const char SeventhpassFile[] = "./dataBase/7.csv";///////////////////
struct player createPlayer();//人物创建
void printData(struct player* player);//展示人物属性
int printStart(int userChoice);//输出开始页面
FILE* openFileroot(const char *linkPass);//文件打开校验
struct monster *passData(const char *linkData);//怪物数据加载
void passDataLoad(int* pass, struct player* player);
void passPrintdata(struct player* player);
void passMonsterdata(struct monster* monster);
int SelectAfter();
void playerUp(struct player* player);
void firstPass(struct player* player);
void secondPass(struct player* player);
int main()
{
	int userChoice;
	struct monster* passMonster = NULL;
	struct player player;
	printf("\t\t\t\t\t|**********************************************************************|\n");
	printf("\t\t\t\t\t|*                      欢迎来到文字修仙(简化版)                      *|\n");
	printf("\t\t\t\t\t|*                                                                    *|\n");
	printf("\t\t\t\t\t|*                                                                    *|\n");
	printf("\t\t\t\t\t|*                                                                    *|\n");
	printf("\t\t\t\t\t|*                            1、开始游戏                             *|\n");
	printf("\t\t\t\t\t|*                            2、历史排行                             *|\n");
	printf("\t\t\t\t\t|*                            3、查看说明                             *|\n");
	printf("\t\t\t\t\t|*                            4、退出游戏                             *|\n");
	printf("\t\t\t\t\t|*                                                                    *|\n");
	printf("\t\t\t\t\t|*                                                                    *|\n");
	printf("\t\t\t\t\t|**********************************************************************|\n");
	printf("\t\t\t\t\t|*所以，道友你的选择是？");
	ret=scanf_s("%d", &userChoice);
	while ((userChoice > 4 || userChoice < 1)||ret!=1)
	{
		while (getchar() != '\n');
		printf("\t\t\t\t\t|*道友别乱数入！！！○( ＾皿＾)っHiahiahia…\n");//校验输入
		printf("\t\t\t\t\t|*所以，道友你的选择是？");
		ret = scanf_s("%d", &userChoice);
	}
	switch (userChoice)
	{
	case 1:
		player = createPlayer();
		printData(&player);
		printf("\t\t\t\t\t|*                     道友开始冒险吧                    *|\n");
		while (true)
		{
			userChoice=printStart(userChoice);
			switch (userChoice)
			{
			case 1:
				passDataLoad(&pass, &player);
				system("cls");
				break;
			case 2:
				printData(&player);
				break;
			case 3:
				printf("\n\t\t\t\t\t 已退出");
				exit(0);
			}
		}
		break;
	case 2:
		break;
	case 3:
		break;
	case 4:
		exit(0);
		break;
	}
	return 0;
}
struct player createPlayer()
{
	int ret=1;
	system("cls");
	struct player player;
	printf("\n\n\n\n");
	printf("\t\t\t\t\t|*设置你的名字(16个字符内)：");
	ret= scanf_s("%s", player.playerName, 20);
	while (ret!=1 ||strlen(player.playerName) > 16)
	{
		system("cls");
		while (getchar() != '\n');
		printf("\n\n\n\n");
		printf("\t\t\t\t\t|*道友别乱数入！○( ＾皿＾)っHiahiahia…   \t\t\t\t*|\n");
		printf("\t\t\t\t\t|*设置你的道号(16个字符内)：");
		ret = scanf_s("%s", player.playerName, 20);
	}
	srand((unsigned)time(NULL));
	player.blood = rand() % 20 + 10;
	player.blue= rand() % 20 + 1;
	player.fightPlayer = rand() % 10 + 5;
	player.money = 100;
	player.exp = 0;
	strcpy_s(player.rank, 7,"练气期");
	player.time = 0;
	strcpy_s(player.playerSkill[0].skillName, 9,"普通攻击");
	player.playerSkill[0].blueDeplete = 0;
	player.playerSkill[0].token = 1;
	player.playerSkill[0].hurtValue = 0;
	for (int i = 1; i < 7; i++)
	{
		player.playerSkill[i].token = 0;
	}
	player.playerEquipment.token = 0;
	player.playerEquipment.fightValue = 0;
	return player;
}
void printData(struct player* player)
{
	system("cls");
	printf("\t\t\t\t\t|*********************************************************|\n");
	printf("\t\t\t\t\t|*————————————————————————————|\n");
	printf("\t\t\t\t\t|*                      角色基础面板                     *|\n");
	printf("\t\t\t\t\t|*————————————————————————————|\n");
	printf("\t\t\t\t\t|*角色名：%-8s\t境界：%s             \t *|\n", player->playerName,player->rank);
	printf("\t\t\t\t\t|*————————————————————————————|\n");
	printf("\t\t\t\t\t|*HP：%-6d\tMP：%-6d\t攻击力：%-4d             *|\n", player->blood, player->blue, player->fightPlayer);
	printf("\t\t\t\t\t|*————————————————————————————|\n");
	printf("\t\t\t\t\t|*灵石：%-10d\tEXP：%-5d                       *|\n", player->money, player->exp);
	printf("\t\t\t\t\t|*————————————————————————————|\n");
	printf("\t\t\t\t\t|*********************************************************|\n");
}
int printStart(int userChoice)
{
	printf("\t\t\t\t\t|*********************************************************|\n");
	printf("\t\t\t\t\t|*1、历练             2、查看面板             3、退出游戏*|\n");
	printf("\t\t\t\t\t|*所以，道友你的选择是？");
	ret = scanf_s("%d", &userChoice);
	while (  ret != 1|| (userChoice > 3 || userChoice < 1))
	{
		while (getchar() != '\n');
		printf("\t\t\t\t\t|*道友别乱数入！！！○( ＾皿＾)っHiahiahia…\n");
		printf("\t\t\t\t\t|*所以，道友你的选择是？");
		ret = scanf_s("%d", &userChoice);
	}
	return userChoice;
}
FILE* openFileroot(const char *linkPass)
{//以读的方式打开数据文件//
	FILE* fileP;
	if (fopen_s(&fileP, linkPass, "r" ))//校验文件是否可打开
	{
		printf("无法打开此文件\n");            //如果打不开，就输出打不开
		exit(0);                               //终止程序
	}
	if (fileP == NULL) {
		printf("Failed to open the file!\n");
		printf("Please check it."); 
		exit(0);
	}
	return fileP;
}
struct monster* passData(const char* linkData)
{
	struct monster* passMonster = NULL,*pr=NULL,*head=NULL;
	FILE* fpassData = openFileroot(linkData);
	char move[200],*mid=NULL,*mid2=NULL;
	fscanf_s(fpassData, "%[^\n]", move,200);
	while (!feof(fpassData)){
		passMonster = (struct monster*)malloc(sizeof(struct monster));
		if (passMonster == NULL)
		{
			printf("节点申请失败！");
			free(passMonster);
			exit(0);
		}
		fscanf_s(fpassData, "%s\n", move,200);
		mid = strtok_s(move, ",",&mid2);
		for (int i = 0; mid; i++)
		{
			if (i == 0)
				strcpy_s(passMonster->monsterName,strlen(mid)+1, mid);
			if (i == 1)
				strcpy_s(passMonster->monsterRank, strlen(mid)+1, mid);
			if (i == 2)
				passMonster->monsterBlood = atoi(mid);
			if (i == 3)
				passMonster->monsterfight = atoi(mid);
			if (i == 4)
				passMonster->exp = atoi(mid);
			if (i == 5)
				passMonster->token = atoi(mid);
			if (i == 6)
				strcpy_s(passMonster->word, strlen(mid)+1, mid);
			mid = strtok_s(NULL, ",",&mid2);
		}
		if (head == NULL)
		{
			head = passMonster;
			pr = passMonster;
		}
		else
		{
			pr->next = passMonster;
			pr = passMonster;
		}
		passMonster->next = NULL;
	}
	fclose(fpassData);
	return head;
}
void passDataLoad(int* pass, struct player* player)
{
	int userChoice;
	if (*pass == 0)
	{
		system("cls");
		printf("\t\t\t\t\t 君若山\n");
		Sleep(1000);
		printf("\t\t\t\t\t 此山常有灵气环绕，山内蕴藏少量低阶灵宝。\n");
		Sleep(1000);
		printf("\t\t\t\t\t 同时，滋生了诸多炼气期的妖魔异兽，更有妖魔首领坐镇于此。\n");
		Sleep(1000);
		printf("\t\t\t\t\t 所以，此处是练气期修士除魔卫道的大好历练之所。\n");
		Sleep(1000);
		firstPass(player);
		playerUp(player);
		*pass+=1;
		printf("\t\t\t\t\t %s：这就是妖兽吗，不过如此。\n\n",player->playerName);
		userChoice = SelectAfter();
		if (userChoice == 2)
			return;
	}
	if (*pass == 1)
	{
		system("cls");
		secondPass(player);
		pass++;
	}
	if (*pass == 2)
	{
		*pass++;
	}
	if (*pass == 3)
	{
		*pass++;
	}
	if (*pass == 4)
	{
		*pass++;
	}
	if (*pass == 5)
	{
		*pass++;
	}
	if (*pass == 6)
	{
		*pass++;
	}
}
void passPrintdata(struct player* player)
{
	printf("\t\t\t\t\t\t\t这是我的第一场战斗加油！！\t\t\t\t\n");
	printf("\t\t\t\t\t|*角色名：%-15s\t境界：%-15s    *|\n", player->playerName, player->rank);
	if (player->playerEquipment.token)
		printf("\t\t\t\t\t|*武器：%-20s*|\n", player->playerEquipment.equipmentName);
	for (int i = 0; i < 7; i++)
	{
		if (player->playerSkill[i].token == 1) {
			printf("\t\t\t\t\t|*技能序号：%d\t%-13s伤害：%-9dMP:%-10d*|\n", i, player->playerSkill[i].skillName, player->playerSkill[i].hurtValue + player->fightPlayer , player->playerSkill[i].blueDeplete);
		}
	}
	printf("\t\t\t\t\t 开始战斗 \t\t\t\t\t\n");
}
void passMonsterdata(struct monster* monster)
{
	printf("\t\t\t\t\t 敌人\n");
	printf("\t\t\t\t\t|*%-30s%-25s*|\n", monster->monsterName, monster->monsterRank);
	printf("\t\t\t\t\t %-55s\n", monster->word);
}
int SelectAfter()
{
	int userChoice;
	printf("\t\t\t\t\t 你现在是要\n");
	printf("\t\t\t\t\t|*1、继续历练               2、返回城镇\t");
	ret = scanf_s("%d", &userChoice);
	while (ret != 1 || (userChoice > 2 || userChoice < 1))
	{
		while (getchar() != '\n');
		printf("\t\t\t\t\t|*道友别乱数入！！！○( ＾皿＾)っHiahiahia…\n");
		printf("\t\t\t\t\t|*所以，道友你的选择是？");
		ret = scanf_s("%d", &userChoice);
	}
	return userChoice;
}
void playerUp(struct player* player)
{
	if (player->exp >= 20)
		strcpy_s(player->rank, 9, "筑基期");
	if(player->exp>=150)
		strcpy_s(player->rank, 9, "结丹期");
	if (player->exp >= 1000)
		strcpy_s(player->rank, 9, "元婴期");
	if (player->exp >= 5000)
		strcpy_s(player->rank, 9, "化神期");
	if (player->exp >= 20000)
		strcpy_s(player->rank, 9, "合体期");
	if (player->exp >= 100000)
		strcpy_s(player->rank, 9, "大乘期");
}
void firstPass(struct player* player)
{
	int skillOrder=0;
	struct player temp=*player;
	struct monster* monster = passData(FirstpassFile);
	passPrintdata(player);
	while (monster)
	{
		passMonsterdata(monster);
		while (monster->monsterBlood>0)
		{
			printf("\t\t\t\t\t|*选择你的技能\t");
			ret = scanf_s("%d", &skillOrder);
			while(ret!=1|| temp.playerSkill[skillOrder].token==0|| skillOrder>=7||(temp.blue-temp.playerSkill[skillOrder].blueDeplete)<0) {
				while (getchar() != '\n');
				printf("\n\t\t\t\t\t|*重新选择你的技能(输入不合法或MP不足)\t");
				ret = scanf_s("%d", &skillOrder);
			}
			monster->monsterBlood -= ( temp.fightPlayer + temp.playerEquipment.fightValue + temp.playerSkill[skillOrder].hurtValue);
			temp.blue -= temp.playerSkill[skillOrder].hurtValue;
			if(monster->monsterBlood>0)
				printf("\t\t\t\t\t|*敌方血量剩余：%d\n", monster->monsterBlood);
			if (monster->monsterBlood <= 0)
			{
				printf("\t\t\t\t\t 你击杀了 %s \n\n", monster->monsterName);
				if (monster->next == NULL)
					Sleep(3000);
				player->money += rand() % 100 + 50;
				player->exp += monster->exp;
				break;
			}
			printf("\t\t\t\t\t 向你发起攻击造成 %d点 伤害\n", monster->monsterfight);
			temp.blood -= monster->monsterfight;
			if (temp.blood < 0)
			{
				printf("游戏结束！");
				exit(0);
			}
		}
		temp = *player;
		monster = monster->next;
	}
}
void secondPass(struct player* player)
{
	
	int skillOrder=0;
	struct player temp = *player;
	struct monster* monster = passData(SecondpassFile);
	passPrintdata(player);
	while (monster)
	{
		passMonsterdata(monster);
		while (monster->monsterBlood > 0)
		{
			printf("\t\t\t\t\t|*选择你的技能\t");
			ret = scanf_s("%d", &skillOrder);
			while (ret != 1 || temp.playerSkill[skillOrder].token == 0 || skillOrder >= 7 || (temp.blue - temp.playerSkill[skillOrder].blueDeplete) < 0) {
				while (getchar() != '\n');
				printf("\n\t\t\t\t\t|*重新选择你的技能(输入不合法或MP不足)\t");
				ret = scanf_s("%d", &skillOrder);
			}
			monster->monsterBlood -= (temp.fightPlayer + temp.playerEquipment.fightValue + temp.playerSkill[skillOrder].hurtValue);
			temp.blue -= temp.playerSkill[skillOrder].hurtValue;
			if (monster->monsterBlood > 0)
				printf("\t\t\t\t\t|*敌方血量剩余：%d\n", monster->monsterBlood);
			if (monster->monsterBlood <= 0)
			{
				printf("\t\t\t\t\t 你击杀了 %s \n\n", monster->monsterName);
				if (monster->next == NULL)
					Sleep(3000);
				player->money += rand() % 1000 + 500;
				player->exp += monster->exp;
				break;
			}
			printf("\t\t\t\t\t 向你发起攻击造成 %d点 伤害\n", monster->monsterfight);
			temp.blood -= monster->monsterfight;
			if (temp.blood < 0)
			{
				printf("游戏结束！");
				exit(0);
			}
		}
		temp = *player;
		monster = monster->next;
	}
}